<?php
// +----------------------------------------------------------------------
// | Tpli [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 栏目模块
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Cate as CateModel;
use app\admin\validate\Cate as CateValidate;
use app\admin\service\Cate as CateService;

class Cate extends Base
{
  public function index()
  {
    $cates = CateModel::getCate();
    $this->assign('cates',$cates);
    return $this->fetch();
  }

  public function publish()
  {
    //获取菜单id
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    $model = new cateModel();
    //是正常添加操作
    if($id > 0) {
      //是修改操作
      if($this->request->isPost()) {
        $post = $this->request->post();
        //验证部分数据合法性
        $validate = new CateValidate();
        if (!$validate->goCheck()) {
            $this->error('提交失败：' . $validate->getError());
        }
        //验证菜单是否存在
        $cate = $model->where('id',$id)->find();
        if(empty($cate)) {
          return $this->error('id不正确');
        }
        if(false == $model->allowField(true)->save($post,['id'=>$id])) {
          return $this->error('修改失败');
        } else {
          addLog($operation_id=$id);//写入日志
          return $this->success('修改分类成功','admin/cate/index');
        }
      } else {
        //非提交操作
        $cate = $model->where('id',$id)->find();
        $cates = $model->select();
        $cates_all = CateService::cateTree($cates);
        $this->assign('cates',$cates_all);
        if(!empty($cate)) {
          $this->assign('cate',$cate);
          return $this->fetch();
        } else {
          return $this->error('id不正确');
        }
      }
    } else {
      //新增操作
      if($this->request->isPost()) {
        //验证部分数据合法性
        $validate = new CateValidate();
        if (!$validate->goCheck()) {
            $this->error('提交失败：' . $validate->getError());
        }
        //提交操作
        $post = $this->request->post();
        if(false == $model->allowField(true)->save($post)) {
          return $this->error('添加失败');
        } else {
          addLog($model->id);//写入日志
          return $this->success('添加成功','admin/cate/index');
        }
      } else {
        //非提交操作
        $pid = $this->request->has('pid') ? $this->request->param('pid', null, 'intval') : null;
        if(!empty($pid)) {
          $this->assign('pid',$pid);
        }
        $cate = $model->select();
        $cates = CateService::cateTree($cate);
        $this->assign('cates',$cates);
        return $this->fetch();
      }
    }
  }

  public function delete()
  {
    if($this->request->isAjax()) {
      $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
      if(CateModel::where('pid',$id)->select() == null) {
          if(false == CateModel::where('id',$id)->delete()) {
              return $this->error('删除失败');
          } else {
              addLog($id);//写入日志
              return $this->success('删除成功','admin/cate/index');
          }
      } else {
          return $this->error('该分类下还有子分类，不能删除');
      }
    }
  }
}
?>